﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using Htss.Platform.WebApi;
using Htss.Platform.Core;
using Htss.Platform.Data;

namespace DreamBox.Api
{
    public class registerBox:ApiBox
    {
        public ActionResult post()
        {
            Verify vf = Verification();
            if (vf.IsVerify)
            {
                Model.User m = new Model.User();
                m.UserName = Htss.Platform.Core.Text.Replace(Request.Form["UserName"]);
                m.Password = Request.Form["Password"];
                m.Email = "htss@buyungu.com";
                DAL.User.UserInfo user = new DAL.User.UserInfo();
                GHashTable hash = user.Add(m);

                if (hash["@ReturnValue"].ToString() == "1")
                {
                    return Json(true,"注册成功");
                }
                else
                {
                    return Json(false, "注册过程中出现异常,您可以尝试重新注册!");
                }
            }
            else
            {
                return Json(false, vf.VerifyDt);
            }
        }
        //验证
        Verify Verification()
        {
            DAL.User.UserInfo user = new DAL.User.UserInfo();

            Verify vf = new Verify();
            VerifyField vUserName = vf.NewVerifyField(Request.Form["UserName"], "UserName");
            vUserName.AddNull("请输入用户名");
            vUserName.AddLengthRange("长度范围在1-20之间", 1, 20);
            vUserName.AddCustom("输入的用户名已经注册,请重新输入", user.IsUserName(Request.Form["UserName"]));

            VerifyField vPassword = vf.NewVerifyField(Request.Form["Password"], "Password");
            vPassword.AddNull("此项不能为空");
            vPassword.AddLengthRange("请输入(6-20)之间的密码长度", 6, 20);

            VerifyField vConfirmPassword = vf.NewVerifyField(Request.Form["ConfirmPassword"], "ConfirmPassword");
            vConfirmPassword.AddNull("此项不能为空");
            vConfirmPassword.AddLengthRange("请输入(6-20)之间的密码长度", 6, 20);
            vConfirmPassword.AddCompare("请输入两次相同的密码", Request.Form["Password"]);

            vf.Begin();
            return vf;
        }
    }
}